from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from .serializers import *
# Create your views here.


class GoodsAdd(APIView):  # 富文本商品添加
    def post(self,request):
        name = request.data.get("name")
        price = request.data.get("price")
        number = request.data.get("number")
        color = request.data.get("color")
        size = request.data.get("size")
        version = request.data.get("version")
        content = request.data.get("content")
        detail = request.data.get("detail")
        if not all([name,price,number,color,size,version,content,detail]):
            data={
                "code":4333,
                "msg":"商品输入不完整，请重新输入"
            }
            return Response(data,status=200)
        else:
            print(name,price,number,color,size,version,
                                        content,detail)
            Goods.objects.create(name=name,price=price,number=number,color=color,size=size,version=version,
                                        content=content,detail=detail)
            last = Goods.objects.filter().last()  # last是最后一条数据
            data={
                "code":200,
                "data":last.detail
            }
            return Response(data,status=200)






class SelectOne(APIView):
    def get(self,request):
        feileiinfo = FenLei.objects.filter(p_id=None)  # p_id为空，查的都是大路由
        fields = FeiLeiSer(feileiinfo,many=True)  # many 是多条数据
        data = fields.data
        res_data = {
            "code":200,
            "data":data,
            "msg":"您所有的父类都在这里"
        }
        return Response(res_data,status=200)

class DaOne(APIView):
    def get(self,request):
        feileiinfo = DaBiao.objects.filter(p_id=None)  # p_id为空，查的都是大路由
        fields = DaBiaoSer(feileiinfo,many=True)  # many 是多条数据
        data = fields.data
        res_data = {
            "code":200,
            "data":data,
            "msg":"您所有的父类都在这里"
        }
        return Response(res_data,status=200)

class Datwo(APIView):
    def get(self,request):
        p_id = request.query_params.get("p_id","")
        print(p_id)
        fenleiinfo = DaBiao.objects.filter(p_id=p_id)
        fields = DaBiaoSer(fenleiinfo, many=True)  # many 是多条数据
        data = fields.data
        res_data = {
            "code": 200,
            "data": data,
            "msg": "您所有的子分类都在这里"
        }
        return Response(res_data, status=200)

class SelectTwo(APIView):
    def get(self,request):
        p_id = request.query_params.get("p_id","")
        print(p_id)
        fenleiinfo = FenLei.objects.filter(p_id=p_id)
        fields = FeiLeiSer(fenleiinfo, many=True)  # many 是多条数据
        data = fields.data
        res_data = {
            "code": 200,
            "data": data,
            "msg": "您所有的子分类都在这里"
        }
        return Response(res_data, status=200)


class GoodsInfoView(APIView):
    def get(self,request):
        id = request.query_params["id"]
        try:
            goodsinfo = Goods.objects.get(id=id)
            goodsser = GoodsSer(goodsinfo)
            data = goodsser.data
            res_data = {
                "code":200,
                "data":data
            }
            return Response(data,status=200)
        except:
            data={
                "code":500
            }
            return Response(data,status=200)
